using System;
using System.Collections.Generic;
using System.Text;

using libsecondlife;
using libsecondlife.InventorySystem;

namespace GliderCLI
{
    public class IncomingSort
    {
        protected SecondLife _Client;
        protected string SortedInboxName = "Sorted Inbox";

        public IncomingSort(SecondLife client)
        {
            _Client = client;

            _Client.Inventory.OnInventoryItemReceived += new libsecondlife.InventorySystem.InventoryManager.On_InventoryItemReceived(Inventory_OnInventoryItemReceived);
        }

        void Inventory_OnInventoryItemReceived(LLUUID fromAgentID, string fromAgentName, uint parentEstateID, LLUUID regionID, LLVector3 position, DateTime timestamp, libsecondlife.InventorySystem.InventoryItem item)
        {
            InventoryFolder sortedBoxFolder = _Client.Inventory.getFolder("/" + SortedInboxName);
            if (sortedBoxFolder == null)
            {
                sortedBoxFolder = _Client.Inventory.GetRootFolder().CreateFolder(SortedInboxName);
                if (sortedBoxFolder == null)
                {
                    Console.WriteLine("There's been a problem with creating the Sorted Inbox: " + SortedInboxName);
                }
            }

            InventoryFolder inboxFolder = _Client.Inventory.getFolder("/" + SortedInboxName + "/" + fromAgentName);
            if (inboxFolder == null)
            {
                inboxFolder = sortedBoxFolder.CreateFolder(fromAgentName);
                if (inboxFolder == null)
                {
                    Console.WriteLine("There's been a problem with creating the Inbox for : " + fromAgentName);
                }
            }

            item.MoveTo(inboxFolder);

        }
    }
}
